﻿Public Class CtrlAdresse
    Inherits System.Web.UI.UserControl


    Public CodePostalInitialisation As String
    Public VilleInitialisation As String

    Public ReadOnly Property Adresse1 As String
        Get
            Return Me.txtAdresse1.Text
        End Get
    End Property
    Public ReadOnly Property Adresse2 As String
        Get
            Return Me.txtAdresse2.Text
        End Get
    End Property
    Public ReadOnly Property Adresse3 As String
        Get
            Return Me.txtAdresse3.Text
        End Get
    End Property
    Public ReadOnly Property Adresse4 As String
        Get
            Return Me.txtAdresse4.Text
        End Get
    End Property
    Public ReadOnly Property CodePostal As String
        Get
            Return Me.txtCodePostal.Text
        End Get
    End Property
    Public ReadOnly Property Ville As String
        Get
            If Me.cbVilleValue.Value = "¤" Then
                Return Me.txtVille.Text
            Else
                Return Me.cbVilleValue.Value
            End If
        End Get
    End Property


    Public Sub Initialiser()
        Me.txtCodePostal.Attributes.Add("onblur", " chargerVilles_" & Me.ClientID & "(null);")
        'ScriptManager.RegisterStartupScript(Me, Me.GetType, "initVille" & Me.ClientID, "init_" & Me.ClientID & "();", True)
    End Sub

    Public Sub AffecterValeurs(adresse1 As String, adresse2 As String,
                               adresse3 As String, adresse4 As String,
                               codePostal As String, ville As String)
        Me.txtAdresse1.Text = adresse1
        Me.txtAdresse2.Text = adresse2
        Me.txtAdresse3.Text = adresse3
        Me.txtAdresse4.Text = adresse4
        CodePostalInitialisation = codePostal
        VilleInitialisation = ville
    End Sub
    Public Sub AffecterValeurs(adresse As ServiceContacts.Adresse)
        AffecterValeurs(adresse.Adresse1, adresse.Adresse2, adresse.Adresse3, adresse.Adresse4, adresse.CodePostal, adresse.Ville)
    End Sub
    Public Sub AffecterValeurs(adresse As ServiceEntreprises.Adresse)
        AffecterValeurs(adresse.Adresse1, adresse.Adresse2, adresse.Adresse3, adresse.Adresse4, adresse.CodePostal, adresse.Ville)
    End Sub

    Public Sub RecupererValeurs(adresse As ServiceContacts.Adresse)
        adresse.Adresse1 = Me.Adresse1
        adresse.Adresse2 = Me.Adresse2
        adresse.Adresse3 = Me.Adresse3
        adresse.Adresse4 = Me.Adresse4
        adresse.CodePostal = Me.CodePostal
        adresse.Ville = Me.Ville
    End Sub
    Public Sub RecupererValeurs(adresse As ServiceEntreprises.Adresse)
        adresse.Adresse1 = Me.Adresse1
        adresse.Adresse2 = Me.Adresse2
        adresse.Adresse3 = Me.Adresse3
        adresse.Adresse4 = Me.Adresse4
        adresse.CodePostal = Me.CodePostal
        adresse.Ville = Me.Ville
    End Sub

    Public Function ValiderCommune() As Boolean
        If Me.txtCodePostal.Text <> "" AndAlso
           Me.cbVilleValue.Value = "¤" Then
            Return EnregistrerCommune(Me.txtCodePostal.Text, Me.txtVille.Text)
        End If

        Return True
    End Function
    Private Function EnregistrerCommune(codePostal As String, ville As String) As Boolean
        Using svc As New ServiceParametre.ParametresClient
            Dim params As New ServiceParametre.CreerCommuneParameters
            params.Authentication = New ServiceParametre.AuthenticationParameters
            params.Authentication.Token = Tools.GetInstance.getToken
            params.Pays = "FR"
            params.CodePostal = codePostal
            params.Departement = codePostal.Substring(0, 2)
            params.Ville = ville

            Try
                Dim results As ServiceParametre.OperationResult = svc.CreerCommune(params)
                If results Is Nothing OrElse Not results.OK Then
                    Tools.NotifyErreur(Me.Page, "Impossible de créer la nouvelle commune" & If(results Is Nothing, ".", " :" & ControlChars.NewLine & results.ErrorMessage))
                Else
                    Return True
                End If
            Catch ex As Exception
                Tools.NotifyErreur(Me.Page, ex.Message)
            End Try
        End Using

        Return False
    End Function

End Class